package common.gui.impl;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.ResourceBundle;

import javax.swing.Action;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import common.ctrl.impl.ControlFactory;
import common.ctrl.impl.HelpCtrl;
import common.gui.IViewable;
import common.i18n.ChangeLanguage;
import common.i18n.IChangeLanguage;
import common.util.FileHandler;

@SuppressWarnings("serial")
public class HelpDialog extends JDialog implements IChangeLanguage, IViewable {

	private JList topics = new JList();
	private JScrollPane spTopics = new JScrollPane(topics);
	private JTextArea text = new JTextArea();
	private JScrollPane spText = new JScrollPane(text);

	public HelpDialog() {
		ChangeLanguage.addObserver(this);
		initControls();
		initComponents();

	}

	private void initComponents() {
		setLanguage(ChangeLanguage.currentLanguage);

		text.setLineWrap(true);
		text.setWrapStyleWord(true);
		text.setEditable(false);
		spText.setPreferredSize(new Dimension(400, 400));
		spTopics.setPreferredSize(new Dimension(200, 400));
		String f = File.separator;
		topics.setListData(FileHandler.loadResources(
				"." + f + 
				"resource" + f +
				"languages" + f + 
				"help_" + 
				ChangeLanguage.currentLanguage.getString("language"), ".help").toArray());

		setSize(new Dimension(610,400));

		add(spText, BorderLayout.WEST);
		add(spTopics, BorderLayout.EAST);

		setVisible(true);
		setResizable(false);
		setModal(true);
		
		//Set the default start location of the frame in the middle of the screen
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation(d.width/2 - getWidth()/2, d.height/2 - getHeight()/2);


	}

	private void initControls() {
		ControlFactory.getControl(HelpCtrl.HELP_DIALOG_CTRL);
		MouseListener ml = (MouseListener) ControlFactory.getControl(HelpCtrl.HELP_DIALOG_CTRL);
		topics.addMouseListener(ml);

		Action a = (Action) ControlFactory.getControl(HelpCtrl.HELP_DIALOG_CTRL);
		a.putValue("HelpDialog.topics", topics);
		a.putValue("HelpDialog.text", text);
	}

	@Override
	public void setLanguage(ResourceBundle rb) {
		setTitle(rb.getString("mnuHelpHelp"));

	}

	@Override
	public void updateView(Object o) {
	}

}
